using System.Text.Json.Serialization;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 命令执行结果基类
    /// 所有命令的返回结果都应继承此类
    /// 作者：huobing
    /// </summary>
    public abstract class BaseCommandResult
    {
        /// <summary>
        /// 命令执行是否成功
        /// </summary>
        [JsonPropertyName("Success")]
        public bool Success { get; set; }

        /// <summary>
        /// 命令执行消息
        /// 成功时包含成功信息，失败时包含错误信息
        /// </summary>
        [JsonPropertyName("Message")]
        public string Message { get; set; }

        /// <summary>
        /// 原始响应数据
        /// 保存命令执行的原始输出
        /// </summary>
        [JsonPropertyName("RawResponse")]
        public string RawResponse { get; set; }

        /// <summary>
        /// 命令名称
        /// </summary>
        [JsonPropertyName("CommandName")]
        public string CommandName { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public BaseCommandResult()
        {
            Message = string.Empty;
            RawResponse = string.Empty;
            CommandName = string.Empty;
        }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <typeparam name="T">命令结果类型</typeparam>
        /// <param name="message">成功消息</param>
        /// <param name="commandName">命令名称</param>
        /// <returns>成功的命令结果</returns>
        public static T CreateSuccessResult<T>(string message, string commandName) where T : BaseCommandResult, new()
        {
            return new T
            {
                Success = true,
                Message = message,
                CommandName = commandName
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="commandName">命令名称</param>
        /// <returns>失败的命令结果</returns>
        public static T CreateErrorResult<T>(string errorMessage, string commandName) where T : BaseCommandResult, new()
        {
            return new T
            {
                Success = false,
                Message = errorMessage,
                CommandName = commandName
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="commandName">命令名称</param>
        /// <returns>失败的命令结果</returns>
        public static BaseCommandResult CreateErrorResult(string errorMessage)
        {
            return new DefaultCommandResult
            {
                Success = false,
                Message = errorMessage,
                CommandName = string.Empty
            };
        }

        /// <summary>
        /// 显示控制台输出
        /// </summary>
        /// <returns>控制台输出内容</returns>
        public virtual string ToConsoleString()
        {
            return Message;
        }
    }

    /// <summary>
    /// 成功命令执行结果
    /// </summary>
    public class DefaultCommandResult : BaseCommandResult
    {

    }
}